/**
 *
 */
package fhq.hcmute.modernshopping.activities;

import static fhq.hcmute.modernshopping.camera.CameraControls.getCameraControls;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import fhq.hcmute.modernshopping.R;
import fhq.hcmute.modernshopping.adapter.Product;
import fhq.hcmute.modernshopping.fragment.PurchaseConfirmationDialog;
import fhq.hcmute.qrcode.QRCode;

/**
 * @author: Hien Nguyen Le
 * @emai: hien.nguyenle.it@gmail.com
 * 
 */
public class ScanQrCodeActivity extends Activity {
	public static int SCAN_SUCCESSFUL = 0;
	public static String SCAN_QRCODE_KEY = "QrCode_Key";

	public static String SCAN_QRCODE_RESULT = "SCAN_QRCODE_RESULT";
	private static Activity activity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shopping_scan_qrcode);
		ScanQrCodeActivity.activity = this;

		SurfaceView surfaceHolder = (SurfaceView) findViewById(R.id.camera_preview);
		getCameraControls().showCameraPreview(getApplicationContext(),
				surfaceHolder.getHolder());
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(onBtnCloseClickListener);
		button.bringToFront();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.scan_qrcode_menu, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		getCameraControls().stopCamera();
		super.onDestroy();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_item_flash_light) {
			if (getCameraControls().toggleFlashLight()) {
				item.setIcon(getResources().getDrawable(
						R.drawable.ic_flashlight_on));
			} else {
				item.setIcon(getResources().getDrawable(
						R.drawable.ic_flashlight_off));
			}
		}
		return super.onOptionsItemSelected(item);
	}

	private View.OnClickListener onBtnCloseClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			getCameraControls().stopCamera();
			finish();
		}
	};

	public static class ScanQrCodeHandler extends Handler {
		private static ScanQrCodeHandler instance = null;

		private ScanQrCodeHandler() {

		}

		public static ScanQrCodeHandler getQrCodeHandler() {
			if (instance == null) {
				instance = new ScanQrCodeHandler();
			}
			return instance;
		}

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == SCAN_SUCCESSFUL) {
				QRCode qrCode = (QRCode) msg.obj;
				Product product = Product.parse(qrCode);
				Intent intent = ScanQrCodeActivity.activity.getIntent();
				int layout = intent.getIntExtra(SCAN_QRCODE_KEY, -1);
				if (layout != -1) {
					if (layout == R.layout.activity_main) {
						intent = new Intent(activity,
								DisplayProductDetailActivity.class);
						intent.putExtra(SCAN_QRCODE_RESULT, product);
						activity.finish();
						activity.startActivity(intent);
					} else if (layout == R.layout.activity_shopping) {
						PurchaseConfirmationDialog dialog = new PurchaseConfirmationDialog(
								"Xác nhận", product);
						dialog.show(activity.getFragmentManager(), "dialog");
					}
				}
			}
			super.handleMessage(msg);
		}
	}
}
